//
//  ZDXTabBarControllerViewController.m
//  ZDXTabBarController180514
//
//  Created by mac on 2018/5/14.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "ZDXTabBarControllerViewController.h"
#import "XJ_HomeViewController.h"
#import "XJ_MyViewController.h"
#import "XJ_AccountBookViewController.h"
#import "XJ_ApplicationViewController.h"
#import "ZDXNavigationViewController.h"
#import "UIImage+image.h"

@interface ZDXTabBarControllerViewController ()<UITabBarControllerDelegate>
@property (nonatomic, assign) NSUInteger selectItem;//选中的item
@end

@implementation ZDXTabBarControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIView*view=[[UIView alloc]init];
    view.backgroundColor=[UIColor whiteColor];
    view.bounds=CGRectMake(0, 13, self.tabBar.bounds.size.width, self.tabBar.bounds.size.height);
    view.frame=self.tabBar.bounds;
    [[UITabBar appearance] insertSubview:view atIndex:0];
    
    self.delegate =self;
    
    self.selectItem = 0; //默认选中第一个
    [self addChildViewControllers];
}
//添加子控制器
- (void)addChildViewControllers{
    
    XJ_HomeViewController *homeVC = [[XJ_HomeViewController alloc] init];
    [self caretTabBarWithImage:[UIImage imageWithOriginalName:@"nav_bg1"] selectedImage:[UIImage imageWithOriginalName:@"nav_bg1_active"] Title:@"首页" Controller:homeVC];
    
    // 分类
    XJ_AccountBookViewController *classVC = [[XJ_AccountBookViewController alloc] init];
    [self caretTabBarWithImage:[UIImage imageWithOriginalName:@"nav_bg2"] selectedImage:[UIImage imageWithOriginalName:@"nav_bg2_active"] Title:@"账本" Controller:classVC];
    
    // 咖啡厅
    XJ_ApplicationViewController *coffeeVC = [[XJ_ApplicationViewController alloc] init];
    [self caretTabBarWithImage:[UIImage imageWithOriginalName:@"nav_bg3"] selectedImage:[UIImage imageWithOriginalName:@"nav_bg3_active"] Title:@"应用" Controller:coffeeVC];
    
    XJ_MyViewController *myVC = [[XJ_MyViewController alloc] init];
    [self caretTabBarWithImage:[UIImage imageWithOriginalName:@"nav_bg4"] selectedImage:[UIImage imageWithOriginalName:@"nav_bg4_active"] Title:@"我的" Controller:myVC];

    
}
- (void)caretTabBarWithImage:(UIImage *)image selectedImage:(UIImage *)selectImage Title:(NSString *)title Controller:(UIViewController*)childVC{
    
    childVC.title = title;
    childVC.tabBarItem.image = image;
    childVC.tabBarItem.selectedImage = selectImage;
    [childVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:Color_ZhuSeDiao} forState:UIControlStateSelected];
    
    ZDXNavigationViewController *baseNav = [[ZDXNavigationViewController alloc] initWithRootViewController:childVC];
    
    [self addChildViewController:baseNav];
    
}

//tabbar选择时的代理
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    self.selectItem = tabBarController.selectedIndex;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSLog(@"--tabbaritem.title--%@",viewController.tabBarItem.title);
    

    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
